<?php

/**
 * This is the model class for table "ham_user_log".
 *
 * The followings are the available columns in table 'ham_user_log':
 * @property integer $id
 * @property integer $user_id
 * @property string $date_time
 * @property string $ip
 * @property string $device
 * @property string $os
 * @property string $browser
 * @property string $browser_version
 *
 * The followings are the available model relations:
 * @property User $id0
 */
class UserLog extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return UserLog the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'ham_user_log';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id', 'required'),
			array('user_id', 'numerical', 'integerOnly'=>true),
			array('ip, browser_version', 'length', 'max'=>45),
			array('device, os, browser', 'length', 'max'=>50),
			array('date_time', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, user_id, date_time, ip, device, os, browser, browser_version', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'id0' => array(self::BELONGS_TO, 'User', 'id'),
		);
	}

	public function beforeSave(){
		if($this->isNewRecord){
			$this->date_time = Date('Y-m-d H:i:s');
		}
		return parent::beforeSave();
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'user_id' => 'User',
			'date_time' => 'Date Time',
			'ip' => 'Ip',
			'device' => 'Device',
			'os' => 'Os',
			'browser' => 'Browser',
			'browser_version' => 'Browser Version',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('date_time',$this->date_time,true);
		$criteria->compare('ip',$this->ip,true);
		$criteria->compare('device',$this->device,true);
		$criteria->compare('os',$this->os,true);
		$criteria->compare('browser',$this->browser,true);
		$criteria->compare('browser_version',$this->browser_version,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public function log($user_id){
		$log = new UserLog;
		$browser = new Browser();
		$log->user_id = $user_id;
		$log->os = $browser->getPlatform();
		$log->browser = $browser->getBrowser();
		$log->browser_version = $browser->getVersion();
		$log->ip = Yii::app()->request->userHostAddress;
		return $log->insert();
	}
}